package lifeOfAFish.scenegraph;

import javax.media.opengl.GL;

/**
 * Simple class for representing material properties
 * @author Erik Bonner
 */
public class sgMaterial {
	private float[] m_specular = new float[4];
	private float[] m_diffuse = new float[4];
	private float[] m_ambient = new float[4];
	private float[] m_emission = new float[4];
	private float[] m_shininess = new float[1];
	
	public sgMaterial()
	{
		float[] v = {1.0f, 1.0f, 1.0f, 1.0f};
		copyArray(m_specular, v);
		copyArray(m_diffuse, v);
		copyArray(m_ambient, v);
		setEmission(0.0f, 0.0f, 0.0f, 0.0f);
		m_shininess[0] = 5.0f;
	}
	
	public void setSpecular(float[] v) {copyArray(m_specular, v);}
	public void setDiffuse(float[] v) {copyArray(m_diffuse, v);}
	public void setAmbient(float[] v) {copyArray(m_ambient, v);}
	public void setEmission(float[] v) {copyArray(m_emission, v);}
	public void setShininess(float v) {m_shininess[0] = v;}
	
	public void setSpecular(float r, float g, float b, float a) {
		m_specular[0] = r;
		m_specular[1] = g;
		m_specular[2] = b;
		m_specular[3] = a;
	}
	public void setDiffuse(float r, float g, float b, float a) {
		m_diffuse[0] = r;
		m_diffuse[1] = g;
		m_diffuse[2] = b;
		m_diffuse[3] = a;
	}
	public void setAmbient(float r, float g, float b, float a) {
		m_ambient[0] = r;
		m_ambient[1] = g;
		m_ambient[2] = b;
		m_ambient[3] = a;
	}
	
	public void setEmission(float r, float g, float b, float a) {
		m_emission[0] = r;
		m_emission[1] = g;
		m_emission[2] = b;
		m_emission[3] = a;
	}
	
	public float[] getSpecular() {return m_specular;}
	public float[] getDiffuse() {return m_diffuse;}
	public float[] getAmbient() {return m_ambient;}
	public float[] getEmission() {return m_emission;}
	public float getShininess() {return m_shininess[0];}
	

	/**
	 * Function for applying the current material properties in 
	 * openGL. 
	 */
	public void apply() {
//		System.out.println("apply");
		GL gl = sgGlobals.gl;
		
		gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, m_ambient, 0);
		gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, m_diffuse, 0);
		gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, m_specular, 0);
		gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, m_emission, 0);
		gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, m_shininess, 0);
		
	}
	
	// small method for copying arrays
	private void copyArray(float[] dest, float[] src){
		for (int i = 0; i < dest.length; i++)
			dest[i] = src[i];
	}
	
	public String toString()
	{
		return 
			"ambient: " + m_ambient[0] + " " + m_ambient[1] + " " + m_ambient[2] + " " + m_ambient[3] + "\n" + 
			"diffuse: " + m_diffuse[0] + " " + m_diffuse[1] + " " + m_diffuse[2] + " " + m_diffuse[3] + "\n" + 
			"specular: " + m_specular[0] + " " + m_specular[1] + " " + m_specular[2] + " " + m_specular[3] + "\n" +
			"shininess: " + m_shininess[0];
	}
	
}
 